#include "SettingsDialog.h"
#include <QDialog>


SettingsDialog::SettingsDialog( QWidget * parent) : QDialog(parent)
{

    KEY_PATH = "path";
    GRAPHICS_PATH = "graphicsPath";
    KEY_INTERVAL = "interval";
    KEY_THREADS = "threads";
    KEY_ADVANCED = "advanced";
    setupUi(this);

    guiSettings = new GUISettings();
    guiSettings->setFile("config.txt");
    values = guiSettings->read();

    this->threadsEdit->setText(values->find(KEY_THREADS).value());
    this->intervalEdit->setText(values->find(KEY_INTERVAL).value());
    this->graphicsEdit->setText(values->find(GRAPHICS_PATH).value());
    this->pathEdit->setText(values->find(KEY_PATH).value());
    if (values->find(KEY_ADVANCED).value() == "1"){
        this->checkBox->setChecked(true);
    }
    else {
        this->checkBox->setChecked(false);
    }

}

void SettingsDialog::on_buttonBox_accepted()
{
    values = new QHash<QString, QString>;
    bool ok1,ok2;
    this->intervalEdit->text().toInt(&ok1);
    this->threadsEdit->text().toInt(&ok2);
    if (ok1 && ok2){
    values->insert(KEY_THREADS, this->threadsEdit->text());
    values->insert(KEY_INTERVAL, this->intervalEdit->text());
    values->insert(GRAPHICS_PATH, this->graphicsEdit->text());
    values->insert(KEY_PATH, this->pathEdit->text());
    if (this->checkBox->isChecked()) {
        values->insert(KEY_ADVANCED, "1");
    }
    else{
        values->insert(KEY_ADVANCED, "0");
    }

    guiSettings->save(values);}
    else {
        QMessageBox msgBox;
        msgBox.setText("Sowohl threads als auch intervall muessen Integer-Zahlen sein. Die config.txt wurde nicht gespeichert.");
        msgBox.exec();
    }

}
